package cn.itcast.ssh.service.impl;

import cn.itcast.ssh.bean.TaskView;
import cn.itcast.ssh.dao.IApproveInfoDao;
import cn.itcast.ssh.dao.IFormDao;
import cn.itcast.ssh.domain.ApproveInfo;
import cn.itcast.ssh.domain.Employee;
import cn.itcast.ssh.domain.Form;
import cn.itcast.ssh.service.FormFlowService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import top.jfunc.common.datetime.DatetimeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiongshiyan at 2021/3/16 , contact me with email yanshixiong@126.com or phone 15208384257
 */
public class FormFlowServiceImpl implements FormFlowService{
    private static final String FORM = "form";
    private IFormDao formDao;
    private IApproveInfoDao approveInfoDao;
    private RuntimeService runtimeService;
    private TaskService taskService;

    @Override
    public void submit(Form form) {
        form.setStatus(Form.STATUS_RUNNING);
        form.setTitle(form.getFormTemplate().getName() + "_" + form.getEmployee().getName() + "_" + DatetimeUtils.toStr(form.getApplyTime(), DatetimeUtils.SDF_DATE));
        formDao.save(form);

        //启动实例
        String processDefinitionKey = form.getFormTemplate().getProcessDefinitionKey();
        Map<String, Object> map = new HashMap<>();
        map.put(FORM, form);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);

        //完成第一个任务
        String taskId = taskService.createTaskQuery().processDefinitionId(processInstance.getId()).singleResult().getId();
        taskService.complete(taskId);
    }

    @Override
    public List<TaskView> getMyTaskViewList(Employee currentUser) {
        List<Task> taskList = taskService.createTaskQuery().taskAssignee(currentUser.getName()).list();
        List<TaskView> taskViews = new ArrayList<>(taskList.size());
        for (Task task : taskList) {
            //查询到流程变量
            Form form = (Form)taskService.getVariable(task.getId(), FORM);
            taskViews.add(new TaskView(task, form));
        }
        return taskViews;
    }

    @Override
    public void approve(ApproveInfo approveInfo, String taskId) {
        approveInfoDao.save(approveInfo);

        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        taskService.complete(taskId);
        Execution execution = runtimeService.createExecutionQuery().executionId(task.getExecutionId()).singleResult();


        Form form = approveInfo.getForm();
        if(!approveInfo.isApproval()){
            //如果本次未同意，流程直接结束，表单状态未通过
            if(null != execution){
                //runtimeService.signal();
            }
            form.setStatus(Form.STATUS_REJECTED);
        }else {
            //本次同意，且流程完结，表单状态为通过
            if(null == execution){
                form.setStatus(Form.STATUS_APPROVED);
            }
        }

        formDao.update(form);
    }

    @Override
    public Form getById(Long id) {
        return formDao.getById(id);
    }

    public IFormDao getFormDao() {
        return formDao;
    }

    public void setFormDao(IFormDao formDao) {
        this.formDao = formDao;
    }

    public IApproveInfoDao getApproveInfoDao() {
        return approveInfoDao;
    }

    public void setApproveInfoDao(IApproveInfoDao approveInfoDao) {
        this.approveInfoDao = approveInfoDao;
    }

    public RuntimeService getRuntimeService() {
        return runtimeService;
    }

    public void setRuntimeService(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }

    public TaskService getTaskService() {
        return taskService;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }
}
